package com.military.service.impl;

import com.military.mapper.EmpMapper;
import com.military.pojo.Emp;
import com.military.pojo.LoginInfo;
import com.military.service.EmpService;
import com.military.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    /**
     * 登录
     * @param emp
     * @return
     */
    @Override
    public LoginInfo login(Emp emp) {
        //1.调用mapper接口,根据用户名和密码查询用户
        Emp e =  empMapper.selectByUsernameAndPassword(emp);

        //2.判断是否存在这个用户,如果存在就组装登录成功信息
        if (e != null){
            log.info("登录成功,用户为:{}",e);

            //生成JWT令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put("id",e.getId());
            claims.put("username",e.getUsername());
            String jwt = JwtUtils.generateToken(claims);
            return new LoginInfo(e.getId(),e.getUsername(),e.getName(),jwt);
        }

        //3.要是不存在,就返回null
        return null;
    }
}
